package com.dyrj.pig.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.pig.bean.TOrder;
import com.dyrj.pig.bean.TSuborder;
import com.dyrj.pig.bean.TSuborderExample;
import com.dyrj.pig.dao.TOrderMapper;
import com.dyrj.pig.dao.TSuborderMapper;
import com.dyrj.pig.service.TOrderService;
import com.dyrj.pig.service.TUserService;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class TOrderServiceImpl implements TOrderService {
    private static SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
	private TOrderMapper tOrderMapper;

    @Resource
    private TSuborderMapper tSuborderMapper;

    @Resource
    private UploadProperties uploadProperties;

    private static String triggerPrefixName = "trigger_";

	@Override
	public PageResultMsg table(TOrder record) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//		record.setUserid(loginUser.getAccount());
		PageInfo<TOrder> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> tOrderMapper.selectList(record));
		return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
	}


	@Override
	public ResultMsg add(TOrder record) {
        String msg = "";
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//        record.set(UUIDUtil.uuid());
        record.setDdsj(sdfd.format(new Date()));
//        record.setUpdatetime(new Date());
//        record.setUserid(loginUser.getAccount());

        try {
            tOrderMapper.insert(record);

        } catch (Exception e) {
        	e.printStackTrace();
            msg = e.getMessage();
        }

        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
	}



	@Override
	public TOrder info(String id) {
        TOrder tUser = tOrderMapper.selectByPrimaryKey(Integer.valueOf(id));
		return tUser;
	}


	@Override
	public TSuborder edit_threeorder(String id) {

        TOrder tOrder = tOrderMapper.selectByPrimaryKey(Integer.valueOf(id));

        TSuborderExample tSuborderExample = new TSuborderExample();
        TSuborderExample.Criteria criteria = tSuborderExample.createCriteria();
        criteria.andDdbmEqualTo(new Integer(id));
        List<TSuborder> listTSuborder = tSuborderMapper.selectByExample(tSuborderExample);

        TSuborder tSuborder = new TSuborder();
        tSuborder.setDdbm(new Integer(id));

        if(tOrder.getAid()!=null){
            tSuborder.setAid(tOrder.getAid().toString());
            tSuborder.setAsj(tOrder.getAsj());
            tSuborder.setAxm(tOrder.getAxm());
            tSuborder.setAzzh(tOrder.getAzzh());
        }

        if(tOrder.getBid()!=null){
            tSuborder.setBid(tOrder.getBid().toString());
            tSuborder.setBsj(tOrder.getBsj());
            tSuborder.setBxm(tOrder.getBxm());
            tSuborder.setBzzh(tOrder.getBzzh());
        }

        if(tOrder.getCid()!=null){
            tSuborder.setCid(tOrder.getCid().toString());
            tSuborder.setCsj(tOrder.getCsj());
            tSuborder.setCxm(tOrder.getCxm());
            tSuborder.setCzzh(tOrder.getCzzh());
        }

        if(tOrder.getDid()!=null){
            tSuborder.setDid(tOrder.getDid().toString());
            tSuborder.setDsj(tOrder.getDsj());
            tSuborder.setDxm(tOrder.getDxm());
            tSuborder.setDzzh(tOrder.getDzzh());
        }

        if(listTSuborder.size()>0){
            for (TSuborder tsuborder:listTSuborder) {
                if("BA".equals(tsuborder.getDdlx())){
                    tSuborder.setBa_yfje(tsuborder.getYfje().toString());
                    tSuborder.setBa_wfje(tsuborder.getWfje().toString());
                }

                if("CB".equals(tsuborder.getDdlx())){
                    tSuborder.setCb_yfje(tsuborder.getYfje().toString());
                    tSuborder.setCb_wfje(tsuborder.getWfje().toString());
                }

                if("DC".equals(tsuborder.getDdlx())){
                    tSuborder.setDc_yfje(tsuborder.getYfje().toString());
                    tSuborder.setDc_wfje(tsuborder.getWfje().toString());
                }
            }
        }
		return tSuborder;
	}
	
	public ResultMsg update(TOrder record) {
        String msg = "";
        try {
            tOrderMapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }

		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
	}
	
	@Override
	public ResultMsg delete(String id) {
        tOrderMapper.deleteByPrimaryKey(Integer.valueOf(id));
		return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
	}

}
